import { defineStore } from 'pinia';
import { store } from '/@/store';
import type { ModifyDeck, DeckCardItem } from '/@/api/gateruler/model/deckModel';
import { cloneDeep } from 'lodash-es';

interface IState {
  createDeck: ModifyDeck;
}

interface IGetter {
  getCreateDeckParams: (state: IState) => IState['createDeck'];
  [propName: string]: ((state: IState) => IState | unknown) | (() => void);
}

interface IActions {
  setDeckId: (id: number) => void;
  setCreateDeckRuler: (data: DeckCardItem) => void;
  setCreateDeckCntCard: (data: DeckCardItem[]) => void;
  setCreateDeckOtherCard: (data: DeckCardItem[]) => void;
  clearCreateDeckParams: () => void;
}

export const useGateRulerStore = defineStore<'app_gateruler', IState, IGetter, IActions>({
  id: 'app_gateruler',
  state: () => {
    return {
      createDeck: {
        id: 0,
        deck_name: '',
        ruler: {},
        cnt_cards: [],
        other_cards: [],
      },
    };
  },

  getters: {
    getCreateDeckParams: (state) => {
      return state.createDeck;
    },
  },

  actions: {
    setDeckId(id: number) {
      this.createDeck.id = id || 0;
    },
    setCreateDeckRuler(data: DeckCardItem) {
      this.createDeck.ruler = cloneDeep(data || {});
    },
    setCreateDeckCntCard(data: DeckCardItem[]) {
      this.createDeck.cnt_cards = cloneDeep(data);
    },
    setCreateDeckOtherCard(data: DeckCardItem[]) {
      this.createDeck.other_cards = cloneDeep(data);
    },
    clearCreateDeckParams() {
      this.createDeck.id = 0;
      this.createDeck.deck_name = '';
      this.setCreateDeckRuler({});
      this.setCreateDeckCntCard([]);
      this.setCreateDeckOtherCard([]);
    },
  },
});

// Need to be used outside the setup
export function useGateRulerStoreWithOut() {
  return useGateRulerStore(store);
}
